package com.yqRecite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqRecite.entity.Admin;
import com.yqRecite.mapper.AdminMapper;
import com.yqRecite.service.AdminService;
import com.yqRecite.util.result.Result;
import com.yqRecite.util.result.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Override
    public List<Admin> getAllAdmins() {
        return adminMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public Admin getAdminById(int adminId) {
        return adminMapper.selectById(adminId);
    }

    @Override
    public Result addAdmin(Admin admin) {
        int res = adminMapper.insert(admin);
        if (res == 1){
            return ResultUtil.ok(admin);
        }else {
            return ResultUtil.error("注册过程中出现未知异常！", admin);
        }
    }

    @Override
    public Result updateAdmin(Admin admin) {
        int res = adminMapper.updateById(admin);
        if (res == 1){
            return ResultUtil.ok(admin);
        }else {
            return ResultUtil.error("修改出现异常", admin);
        }
    }

    @Override
    public int deleteAdmin(int adminId) {
        return adminMapper.deleteById(adminId);
    }

    @Override
    public Admin getAdminByName(String username) {
        return adminMapper.getAdminByName(username);
    }

    @Override
    public Result login(String username, String password) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("password", password);
//        Admin res = adminMapper.searchAdminByNameAndPwd(username, password);
        Admin res = adminMapper.selectOne(wrapper);
        System.out.println("res: "+ res);
        if (res == null){
            return ResultUtil.fail("登录失败，请检查用户名和密码", res);
        }
        System.out.println("登录成功！admin: " + res);
        return ResultUtil.ok(res);
    }

}
